package com.ktg.mes.md.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ktg.common.core.domain.APIResult;
import com.ktg.mes.md.domain.MdBatchContent;
import com.ktg.mes.md.domain.MesScalesOrderItem;
import com.sun.org.apache.xpath.internal.objects.XNumber;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktg.common.annotation.Log;
import com.ktg.common.core.controller.BaseController;
import com.ktg.common.enums.BusinessType;
import com.ktg.common.core.domain.Result;
import com.ktg.mes.md.domain.MdBatchHeader;
import com.ktg.mes.md.service.IMdBatchHeaderService;
import com.ktg.mes.md.service.IMdBatchContentService;
import com.ktg.common.utils.poi.ExcelUtil;

/**
 * 产品批次单Controller
 *
 * @author byteco
 * @date 2022-12-15
 */
@RestController
@RequestMapping("/md/mdBatchHeader")
public class MdBatchHeaderController extends BaseController
{
    @Autowired
    private IMdBatchContentService mdBatchContentService;
    @Autowired
    private IMdBatchHeaderService mdBatchHeaderService;

    /**
     * 查询产品批次单列表
     */
    @PreAuthorize("@ss.hasPermi('md:mdBatchHeader:list')")
    @GetMapping("/list")
    public Result<IPage<MdBatchHeader>> list(MdBatchHeader mdBatchHeader,
                                            @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                            @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                            HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("查询产品批次单列表",req,null);
        try {
            Page<MdBatchHeader> page = new Page<MdBatchHeader>(pageNo, pageSize);
            IPage<MdBatchHeader> pageList = mdBatchHeaderService.queryPageList(mdBatchHeader,page);
            return apiLog.ok(pageList);
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 导出产品批次单列表
     */
    @PreAuthorize("@ss.hasPermi('md:mdBatchHeader:export')")
    @Log(title = "产品批次单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MdBatchHeader mdBatchHeader,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("导出产品批次单列表",req,mdBatchHeader);
        try {
            List<MdBatchHeader> list = mdBatchHeaderService.queryList(mdBatchHeader);
            ExcelUtil<MdBatchHeader> util = new ExcelUtil<MdBatchHeader>(MdBatchHeader.class);
            util.exportExcel(response, list, "产品批次单数据");
            apiLog.ok(null);
        }catch (Exception e){
            apiLog.error(e);
        }
    }

    /**
     * 获取产品批次单详细信息
     */
    @PreAuthorize("@ss.hasPermi('md:mdBatchHeader:query')")
    @GetMapping(value = "/{recordId}")
    public Result<MdBatchHeader> getInfo(@PathVariable("recordId") Long recordId,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("获取产品批次单详细信息",req,null);
        try {
            return apiLog.ok(mdBatchHeaderService.getInfo(recordId));
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 新增产品批次单
     */
    @PreAuthorize("@ss.hasPermi('md:mdBatchHeader:add')")
    @Log(title = "产品批次单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Result<MdBatchHeader> add(@RequestBody MdBatchHeader mdBatchHeader,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("新增产品批次单",req,mdBatchHeader);
        try {
            MdBatchContent arr =new MdBatchContent();
            arr.setBatchNo(mdBatchHeader.getBatchNo());
            arr.setPrefix(mdBatchHeader.getPrefix());

            MdBatchContent list = mdBatchContentService.findLastSerialNumber(arr);
//            return apiLog.ok(list);
            if (list!=null) {
                mdBatchHeader.setStartNumber(list.getSerialNumber()+1);
                mdBatchHeader.setEndNumber(list.getSerialNumber()+mdBatchHeader.getQuantity());
            }else{
                mdBatchHeader.setStartNumber((long)1);
                mdBatchHeader.setEndNumber(mdBatchHeader.getQuantity());
            }

            MdBatchHeader task=mdBatchHeaderService.insert(mdBatchHeader);

            for (int i = 0; i < mdBatchHeader.getQuantity(); i++) {
                MdBatchContent arr2 =new MdBatchContent();
                arr2.setBatchHeaderId(task.getRecordId());
                arr2.setSerialNumber( mdBatchHeader.getStartNumber()+i);
                arr2.setPrintNumber((long)0);
                mdBatchContentService.insert(arr2);
            }
            return apiLog.ok("添加成功！");
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 修改产品批次单
     */
    @PreAuthorize("@ss.hasPermi('md:mdBatchHeader:edit')")
    @Log(title = "产品批次单", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public Result<String> edit(@RequestBody MdBatchHeader mdBatchHeader,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("修改产品批次单",req,mdBatchHeader);
        try {
            mdBatchHeaderService.update(mdBatchHeader);
            return apiLog.ok("编辑成功！");
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 删除产品批次单
     */
    @PreAuthorize("@ss.hasPermi('md:mdBatchHeader:remove')")
    @Log(title = "产品批次单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recordIds}")
    public Result<String> remove(@PathVariable Long[] recordIds,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("删除产品批次单",req,null);
        try {
            mdBatchHeaderService.delete(recordIds);
            return apiLog.ok("删除成功！");
        }catch (Exception e){
            return apiLog.error(e);
        }

    }
}
